package xyz.scootaloo.oj.leetcode

import org.junit.Test
import xyz.scootaloo.common.TestDsl

/**
 * @author flutterdash@qq.com
 * @since 2021/7/10 14:54
 */
class P242 : TestDsl {

    private fun isAnagram(s: String, t: String): Boolean {
        if (s.length != t.length)
            return false
        val cacheOfS = IntArray(26).apply {
            for (ch in s) {
                val pos = ch - 'a'
                this[pos]++
            }
        }

        val cacheOfC = IntArray(26).apply {
            for (ch in t) {
                val pos = ch - 'a'
                this[pos]++
            }
        }

        // check
        for (i in cacheOfC.indices) {
            if (cacheOfS[i] != cacheOfC[i])
                return false
        }

        return true
    }

    @Test
    fun test0() {
        isAnagram("anagram", "nagaram") shouldBe true
        isAnagram("rat", "car") shouldBe false
    }

}
